#include<event2/event.h>
#include<signal.h>
#include<iostream>
using namespace std;
static timeval t1={1,0};
void timer1_cb(int sock,short whice,void *arg)
{
    cout<<"timer"<<flush;
    event *ev=(event*)arg;
    if(!evtimer_pending(ev,&t1))
    {
        evtimer_del(ev);
        evtimer_add(ev,&t1);
    }
}
void timer2_cb(int sock,short whice,void *arg)
{
    cout<<"timer2"<<flush;
    event *ev=(event*)arg;
  
}
void timer3_cb(int sock,short whice,void *arg)
{
    cout<<"timer3"<<flush;
    event *ev=(event*)arg;
  
}
int main(int argc,char **argv)
{
    // event_config *ec= event_config_new();
    // event_base *eb=event_base_new_with_config(ec);

    // event_base_free(eb);
    // event_config_free(ec);
    if(signal(SIGPIPE,SIG_IGN)==SIG_ERR)
    {
        return 1;
    }
    event_base *eb=event_base_new();
    //非持久定时器 需重新添加
    event *evl=evtimer_new(eb,timer1_cb,event_self_cbarg());
    if(!evl)
    {
        cerr<<"evtimer_new failed"<<endl;
        return -1;
    }
    static timeval t2;
    t2.tv_sec=0;//秒
    t2.tv_usec=20000;//微秒
    event *ev2=event_new(eb,-1,EV_PERSIST,timer2_cb,event_self_cbarg());
    //evtimer_add(evl,&t1);
    evtimer_add(ev2,&t2);//插入性能O(Log(n))
    //超时优化，默认event使用的是二叉堆（完全二叉树）存储O(log（n）)，
    //优化到双向队列插入删除O(1)
    event *ev3=event_new(eb,-1,EV_PERSIST,timer3_cb,event_self_cbarg());
    static timeval tv_in={3,0};
    const timeval *t3;
    t3=event_base_init_common_timeout(eb,&tv_in);
    event_add(ev3,t3);//插入性能O(1)

    //进入事件主循环
    event_base_dispatch(eb);
    event_base_free(eb);
    
    return 0;
}